<template>
	<view >
		<view class="wrap ">
				<u-swiper :list="slides" name="img_url" height="350"></u-swiper>
			</view>
		<view class="u-m-t-30">
			<u-tabs :list="sortList" :is-scroll="false" :current="current" @change="change"></u-tabs>
		</view>
		<u-row gutter="16" class="u-skeleton">
			<u-col span="6" v-for="(i,index) in goods" :key="index">
				<view class="">
					<goods-card :i="i"></goods-card>
				</view>
			</u-col>			
		</u-row>
		<!--引用骨架屏组件-->
		<u-skeleton :loading="loading" :animation="true" bgColor="#FFF"></u-skeleton>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				sortList: [{
					name: '默认'
				}, {
					name: '销量'
				}, {
					name: '推荐',
				}, {
					name: '最新',
				}],
				current: 0,
				// 轮播图
				slides: [],
				// 商品列表
				goods: [{},{},{},{}],
				// 分页
				page: 1,
				// 骨架屏的显示与隐藏
				loading: false,
				goodsId: null
			}
		},
		// 生命周期相关
		onLoad(option) {	
			this.getData()
		},
		methods: {
			change(index) {
				this.current = index;
				// 重置商品数据和分页
				this.goods = [{},{},{},{}]
				this.page = 1
				this.getData()
				
			},
			// 获取数据
			async getData(){
				// 显示骨架屏
				this.loading = true
				const params = {
					page: this.page
				}
				// 增加排序条件
				if(this.current == 1) params.sales = 1
				if(this.current == 2) params.recommend = 1
				if(this.current == 3) params.new = 1
				const res = await this.$u.api.getIndex(params)
				// 隐藏骨架屏
				this.loading = false
				// console.log(res)
				const {goods,slides} = res
				this.goods = this.goods.pop().title ? [...this.goods,...goods.data] : goods.data
				this.slides = slides
			}
		},
		onReachBottom() {
			// console.log(111)
			// 重新请求首页数据  分页数据
			this.page = this.page + 1
			this.getData()
		}
	}
</script>

<style lang="scss" scoped>
	
</style>
